package com.dgutnw.autoconfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableConfigurationProperties(CustomProperties.class)
@ConditionalOnProperty(prefix="yzy.auto",name="enable",havingValue = "true")
public class AutoConfig {
    @Bean
    public RemoteCommandLineRunner remoteCR(){
        return new RemoteCommandLineRunner();
    }


    /**
     *    ApplicationReadyEvent的事件监听器
     */
    @Bean
    public ApplicationListener<ApplicationReadyEvent> listener(){
        return new ApplicationListener<ApplicationReadyEvent>() {
            @Override
            public void onApplicationEvent(ApplicationReadyEvent event) {
                System.out.println("这是一个自定义的 ApplicationReadyEvent的事件监听器");
            }
        };
    }

    /**
     *    自定义的事件发生器
     */
    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor taskExeuctor){
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExeuctor);
        return eventMulticaster;
    }

}
